﻿using UnityEngine;
using System.Collections.Generic;

public class UIInterLayer : UIWidget
{
    private List<Renderer> childRenders = new List<Renderer>();

    [HideInInspector]
    [SerializeField]
    Shader mShader;
    public int RenderQueue
    {
        get
        {
            if (drawCall != null)
            {
                return drawCall.finalRenderQueue;
            }
            else
            {
                return -1;
            }
        }
    }
    private int preQueue = 0;
    private bool preVisible = false;

    public override Shader shader
    {
        get
        {
            if (mShader == null) mShader = Shader.Find("Unlit/Transparent Colored");
            return mShader;
        }
    }
    public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        int offset = verts.size;

        verts.Add(Vector3.zero);
        verts.Add(Vector3.zero);
        verts.Add(Vector3.zero);
        verts.Add(Vector3.zero);

        uvs.Add(Vector2.zero);
        uvs.Add(Vector2.zero);
        uvs.Add(Vector2.zero);
        uvs.Add(Vector2.zero);

        Color32 c = color;
        cols.Add(c);
        cols.Add(c);
        cols.Add(c);
        cols.Add(c);

        if (onPostFill != null)
            onPostFill(this, offset, verts, uvs, cols);
    }

    protected override void OnUpdate()
    {
        base.OnUpdate();
        bool visible = isVisible;
        if (preVisible != visible)
        {
            preVisible = visible;
            SwitchChild(visible);
        }
        int q = RenderQueue;
        if (preQueue != q)
        {
            preQueue = q;
            if (q == -1)
            {
                preVisible = false;
                SwitchChild(preVisible);
            }
            else
            {
                SetChildRenderQueue(q);
            }
        }
    }

    protected override void OnStart()
    {
        Debug.Log("start");
        base.OnStart();
        childRenders.Clear();
        GetChildRenderer(transform);
        Debug.Log(childRenders.Count);
    }

    private void SwitchChild(bool isOn)
    {
        int cc = transform.childCount;
        for (int i = 0; i < cc; ++i)
        {
            transform.GetChild(i).gameObject.SetActive(isOn);
        }
    }

    private void SetChildRenderQueue(int queue)
    {
        for (int i = 0; i < childRenders.Count; ++i)
        {
            if (Application.isPlaying)
            {
                childRenders[i].material.renderQueue = queue;
            }
            else
            {
                childRenders[i].sharedMaterial.renderQueue = queue;
            }
        }
    }

    private void GetChildRenderer(Transform tran)
    {
        Renderer r = tran.GetComponent<Renderer>();
        if (r != null)
        {
            childRenders.Add(r);
        }
        int cc = tran.childCount;
        for (int i = 0; i < cc; ++i)
        {
            GetChildRenderer(tran.GetChild(i));
        }
    }
    public void ChildChange()
    {
        childRenders.Clear();
        GetChildRenderer(transform);
    }
}
